﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;
using Microsoft.Win32;
using XamlLocalizer.Parsing;
using XamlLocalizer.Tools;
using Path=System.IO.Path;

namespace XamlLocalizer
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            textBox1.Text = Properties.Settings.Default.LastFile;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            var dlg = new OpenFileDialog();
            dlg.DefaultExt = ".xaml";
            dlg.Filter = "*.xaml|*.xaml";
            //dlg.InitialDirectory = 
            dlg.RestoreDirectory = true;
            if (dlg.ShowDialog().Value)
            {
                textBox1.Text = dlg.FileName;
            }
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            Properties.Settings.Default.LastFile = textBox1.Text;

            //XamlExtractor ext = new XamlExtractor(textBox1.Text);

            //ParseResult parseResult = ext.Parse();

            //ext.SaveReplaced("C:\\Temp\\" + System.IO.Path.GetFileName(textBox1.Text));

            //new Preview().ShowDialog();

            listBox1.Items.Clear();

            foreach (string subFolder in Directory.GetDirectories(textBox1.Text))
            {
                ParseXamlInFolders(subFolder, true);
            }
        }

        private void ParseXamlInFolders(string fullFolderPath, bool isRoot)
        {
            string folder = Path.GetFileName(fullFolderPath);
            if (isRoot &&
                (folder.EndsWith(".Test", StringComparison.InvariantCultureIgnoreCase) ||
                !folder.StartsWith("Primus.", StringComparison.InvariantCultureIgnoreCase)))
            {
                return;
            }
            if (!isRoot && String.Compare(folder, "bin", StringComparison.InvariantCultureIgnoreCase) == 0)
            {
                return;
            }

            foreach (string subFolder in Directory.GetDirectories(fullFolderPath))
            {
                ParseXamlInFolders(subFolder, false);
            }

            foreach (string file in Directory.GetFiles(fullFolderPath, "*.xaml"))
            {
                CheckXamlFile(file);
            }
        }

        private void CheckXamlFile(string file)
        {
            var ext = new XamlExtractor(File.ReadAllText(file));
            if (ext.ParseResult.NewItems.Count > 0)
            {
                listBox1.Items.Add(file);
            }
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            doc.PreserveWhitespace = true;
            doc.Load(textBox1.Text);

            var wrt = PrettyXmlWriter.Create();
            doc.WriteTo(wrt);
            //MessageBox.Show(wrt.GetXml());
            File.WriteAllText("c:\\temp\\tst.xml", wrt.GetXml());
        }
    }
}
